<?php

namespace app\api\controller;

use app\common\BaseHelper;

use app\admin\model\Userprojectchat as UserprojectchatMdl;
use app\admin\model\User;
use app\admin\model\Member;
use app\admin\model\Image;

use think\Db;
use think\Exception;
use think\Request;

class Userprojectchat extends BaseController
{

    /**
     * 列表
     */
    public function getMess()
    {
        $param = self::getHttpParam();

        $start = 0;
        $length = 20;

        if (empty($param->projectId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'projectId 不能为空');
        }
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $projectId = $param->projectId;
        $where = [
            'projectId' => $projectId
        ];

        $model = new UserprojectchatMdl();
        $fields = '*';
        $order = 'createDate DESC';
        $result = $model->where($where)->field($fields)->limit($start, $length)->order($order)->select();
        $total = $model->where($where)->count();
        if (!$result) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '没有相关数据');
        }

        foreach ($result as $item) {
            if($item['imgUrls']){
                $item['imgUrls'] = explode(',', $item['imgUrls']);
            }
            // 发送人信息
            $userInfo = User::where(['id' => $item['userId']])->field('memberId, nickName as name, avatarUrl as avatar')->find();
            if($userInfo['memberId']){
                $userInfo = Member::where(['id' => $userInfo['memberId']])->field('name, avatar')->find();
            }
            $item['userInfo'] = $userInfo;
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result, $total);
    }

    /**
     * 发送消息
     */
    public function createMess()
    {
        $param = self::getHttpParam();

        if (empty($param->townId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'townId 不能为空');
        }
        if (empty($param->projectId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '项目ID(projectId) 不能为空');
        }
        /*if (empty($param->content)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '内容(content) 不能为空');
        }*/
        /*if (empty($param->imgUrls)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'imgUrls 不能为空');
        }*/
        /*if (empty($param->videoUrl)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'videoUrl 不能为空');
        }*/

        try {
            $data = (array)$param;
            if(!empty($param->imgUrls)){
                $data['imgUrls'] = implode(',', $param->imgUrls);
            }
            $model = new UserprojectchatMdl();

            $data['id'] = BaseHelper::getUUID();
            $data['createDate'] = time();

            $result = $model->save($data);
            if($result === false){
                return show(config('status.ERROR_STATUS'), '发送失败', $model->getError());
            }
        } catch (Exception $e) {
            return show(config('status.ERROR_STATUS'), '发送失败', $e->getMessage());
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $data);
    }

}